package user_center.user.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cwb.consts.RequestPrefix;
import cwb.dto.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import user_center.user.pojo.SystemUser;
import user_center.user.service.impl.SystemUserServiceImpl;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Ezio.Auditore
 * @since 2021-12-20
 */
@RestController
@RequestMapping(RequestPrefix.API + "/user")
@Api(value = "系统用户", tags = "系统用户", description = "系统用户")
public class SystemUserController {

    @Autowired
    private SystemUserServiceImpl systemUserService;

    @PostMapping("/login")
    @ApiOperation(value = "系统用户登录", notes = "系统用户登录", response = String.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String")
    })
    public String login(@RequestParam("username") String username, @RequestParam("password") String password) {
        return systemUserService.login(username, password);
    }

    /**
     * 根据名称获取用户信息
     *
     * @param username
     * @return
     */
    @PostMapping("/getByName")
    SystemUser getByName(@RequestParam("username") String username) throws Exception {
        return systemUserService.getByName(username);
    }

    /**
     * 查看系统用户详情
     *
     * @param userId
     * @return
     */
    @GetMapping("/detail")
    @ApiOperation(value = "系统用户查询详情", notes = "系统用户查询详情", response = SystemUser.class)
    public ResultData getUser(Integer userId) {
        return ResultData.buildSucess(systemUserService.getById(userId));
    }

    /**
     * 新增系统用户
     *
     * @param systemUser
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增系统用户", notes = "新增系统用户", response = SystemUser.class)
    public ResultData addUser(SystemUser systemUser) {
        try {
            return ResultData.buildSucess(systemUserService.addNewUser(systemUser));
        } catch (Exception e) {
            return ResultData.buildError(e.getMessage());
        }
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除系统用户", notes = "删除系统用户", response = SystemUser.class)
    public ResultData deleteUser(SystemUser systemUser) {
        try {
            systemUser.setUserStatus(0);
            return ResultData.buildSucess(systemUserService.updateById(systemUser));
        } catch (Exception e) {
            return ResultData.buildError(e.getMessage());
        }
    }

    /**
     * 查看系统用户列表
     *
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查看系统用户列表", notes = "查看系统用户列表", response = SystemUser.class)
    public ResultData pageUser(@RequestParam Integer pageSize, @RequestParam Integer pageNum, SystemUser systemUser) {
        try {
            return ResultData.buildSucess(systemUserService.page(new Page(pageNum, pageSize)));
        } catch (Exception e) {
            return ResultData.buildError(e.getMessage());
        }
    }

}
